package com.fangque.kepp.framework.config;

import com.fangque.kepp.framework.config.filter.JwtAuthenticationFilter;
import com.fangque.kepp.framework.config.filter.JwtLoginFilter;
import com.fangque.kepp.framework.config.handle.AuthEntryPoint;
import com.fangque.kepp.framework.config.handle.RestAuthenticationAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author Lan
 * @date 2019/4/8 15:22
 * @description security配置
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserDetailsService userService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.cors().disable().csrf().disable().authorizeRequests()
//                .anyRequest().permitAll();
        http.authorizeRequests()
                .antMatchers("/user/**").permitAll()
                .anyRequest()
                .access("@judgeAuthorityService.hasPermission(request,authentication)")
//                .and().formLogin().loginPage("/index.html").loginProcessingUrl("/user/login").permitAll()
                .and()
                .addFilter(new JwtLoginFilter(authenticationManager()))
                .addFilter(new JwtAuthenticationFilter(authenticationManager()))
                .exceptionHandling().accessDeniedHandler(new RestAuthenticationAccessDeniedHandler())
                .authenticationEntryPoint(new AuthEntryPoint())
                .and().csrf().disable().cors().disable();
/*        http.cors().and().csrf().disable().authorizeRequests()
                .antMatchers("/user/hello").permitAll()
                .antMatchers("/user/logout").permitAll()
//                .anyRequest().authenticated()
                .anyRequest()
                .access("@judgeAuthorityService.hasPermission(request,authentication)")
                .and()
                .addFilter(new JwtLoginFilter(authenticationManager()))
                .addFilter(new JwtAuthenticationFilter(authenticationManager()))
                .exceptionHandling().accessDeniedHandler(new RestAuthenticationAccessDeniedHandler())
                .authenticationEntryPoint(new AuthEntryPoint());*/
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/","/index.html", "/static/**", "/favicon.ico");
    }
}
